(function () {
    //定义全局变量对象
    var account = document.getElementById('account');
    var nickname = document.getElementById('user_nick_name');
    var password = document.getElementById('password');
    var passwordVali = document.getElementById('password_validate');
    var passwordValiText = document.getElementById('password_validate_textfield');
    var tel = document.getElementById('tel');
    var email = document.getElementById('email');
    var register = document.getElementById('register_input');
    var login = document.getElementById('login');

    register.onclick = function () {
        userRegister();
    };

    var userRegister = function () {
        var passwordValiTextClass = passwordValiText.classList;
        if (account.value.length <= 0 || nickname.value.length <= 0){
            mdui.alert('信息错误');
            return;
        }
        var psd = password.value;
        var psdVali = passwordVali.value;
        console.log('password: ' + psd);
        console.log('passwordVali' + psdVali);
        if (psd !== psdVali) {
            passwordValiTextClass.add('mdui-textfield-invalid');
            return;
        } else {
            passwordValiTextClass.remove('mdui-textfield-invalid');
        }

        Api.ajax({
            method: 'POST',
            url: '/logup',
            data: {
                account: account.value,
                nickname: nickname.value,
                password: password.value,
                tel: tel.value,
                email: email.value
            },
            success: function (response) {
                console.log(response);
                if (response.code == 0) {
                    //TODO 跳转
                    //使用弹窗跳转 openLogin();

                } else {
                    mdui.alert('注册失败', null, null, '重新注册');
                }
            }
        })
    };
    
    function openLogin() {
        self.location.href = 'login.html';
    }

})();